const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

function formatDate(timestamp) {
  var date = new Date(timestamp);
  var year = date.getFullYear(); // 获取年份
  var month = date.getMonth() + 1; // 获取月份，注意月份是从0开始的，所以要加1
  var day = date.getDate(); // 获取日
  return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function timestampToTime(value, type = 0){
  var time = new Date(value);
  var year = time.getFullYear();
  var month = time.getMonth() + 1;
  var date = time.getDate();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  month = month < 10 ? "0" + month : month; 
  date = date < 10 ? "0" + date : date; 
  hour = hour < 10 ? "0" + hour : hour; 
  minute = minute < 10 ? "0" + minute : minute; 
  second = second < 10 ? "0" + second : second; 
  var arr = [ 
      year + "-" + month + "-" + date, 
      year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second, 
      year + "年" + month + "月" + date, 
      date + "天" + hour + "时" + minute + "分", 
      hour + ":" + minute + ":" + second 
  ] 
  return arr[type]; 
} 
function formatTime1(date) {
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();

  hour = hour < 10 ? '0' + hour : hour;
  minute = minute < 10 ? '0' + minute : minute;

  return [hour, minute].join(':');
}


module.exports = {
  formatTime,
  timestampToTime,
  formatDate,
  formatTime1
}
